package question.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.KeyBoard;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/4/10.
 * 发送问题评论
 */
@ContentView(R.layout.deliver_question_comment)
public class DeliverQuestionComment extends BaseActivity implements View.OnClickListener{
    @ViewInject(R.id.deliver_question_comment_title)
    private TextView deliver_question_comment_title;
    @ViewInject(R.id.deliver_question_comment_back)
    private ImageView deliver_question_comment_back;
    @ViewInject(R.id.deliver_question_comment_finish)
    private TextView deliver_question_comment_finish;
    @ViewInject(R.id.deliver_question_comment_number)
    private TextView deliver_question_comment_number;
    @ViewInject(R.id.deliver_question_comment_content)
    private EditText deliver_question_comment_content;

    private String content;//输入的内容
    private String dialogId;
        private Handler handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what) {
                        case Constant.ERROR_CODE_SUCCESS://成功返回
                            try {
                                JSONObject jsonObject = new JSONObject((String) msg.obj);
                                String err_code = jsonObject.getString("err_code");
                                if("0".equals(err_code)){
                                    //成功
                                    ToastUtils.shortToast("评论成功");
                                    Intent i = new Intent();
                                    setResult(1,i);
                                    DeliverQuestionComment.this.finish();
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            break;
                        default:
                            break;
                    }
                }
            };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialogId = getIntent().getStringExtra("dialogId");
        initView();
        KeyBoard.showKeyBoard();
    }


    private void initView() {
        deliver_question_comment_finish.setOnClickListener(this);
        deliver_question_comment_back.setOnClickListener(this);
        deliver_question_comment_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 255) {
                    deliver_question_comment_content.setText(content);
                    deliver_question_comment_content.setSelection(content.length());
                    ToastUtils.shortToast("输入字数已达上限");
                } else {
                    content = s.toString();
                }
                deliver_question_comment_number.setText("" + content.length());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.deliver_question_comment_finish:
                if (!TextUtils.isEmpty(content)) {
                    CommitComment();
                }
                break;
            case R.id.deliver_question_comment_back:
                this.finish();
                break;
            default:
                break;
        }
    }

    private void CommitComment() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("content",content);
        hashMap.put("dialogid",dialogId);
        new InterNetController(this, Constant.COMMENTDIALOG, handler, hashMap, 0);
    }
}
